﻿//初始化
$(function () {
    //回车键
    document.onkeydown = function (e) {
        if (!e) e = window.event;
        if ((e.keyCode || e.which) == 13) {
            var btnlogin = document.getElementById("btnlogin");
            if (btnlogin != null) {
                btnlogin.click();
            }
            var btnSignIn = document.getElementById("btnSignIn");
            if (btnSignIn != null) {
                btnSignIn.click();
            }
        }
    }

    function loadTopWindow() {
        if (window.top != null && window.top.document.URL != document.URL) {
            window.top.location = document.URL; //这样就可以让登陆窗口显示在整个窗口了
        }
    }
    $("#bodytop").load(loadTopWindow());

    //登录按钮事件
    $("#btnlogin").click(function () {
        var userLoginName = $.trim($("#userLoginName").val());
        var userPwd = $.trim($("#userPwd").val());
        var code = $.trim($("#code").val());
        
        if (userLoginName == "") {
            ErrorMessage($("#userLoginName"), '请输入账号');
        } else if (userPwd == "") {
            ErrorMessage($("#userPwd"), '请输入密码');
        } else if (code == "") {
            ErrorMessage($("#login_verifycode"), '请输入验证码');
            $("#code").focus();
        } else {
            var data = {
                userLoginName: userLoginName,
                userPwd: userPwd,
                verifyCode: code
            };
            CheckLogin(data);
        }
    });
    function CheckLogin(data) {
        $.ajax({
            type: 'POST',
            url: '/Login/Checked',
            data: data,
            dataType: 'JSON',
            success: function (data) {
                if (data.type == 1) {
                    window.location.href = '/Home/Index';
                } else {
                    ExistMessage(data);
                    $("#login_verifycode").trigger("click");
                }
            }
        });
    }


    //注册事件
    $("#btnSignIn").click(function () {
        var userLoginName = $.trim($("#userLoginName").val());
        var userEmail = $.trim($("#userEmail").val());
        var userName = $.trim($("#userName").val());
        var userPwd = $.trim($("#userPwd").val());
        var userPwd1 = $.trim($("#userPwd1").val());
        var code = $.trim($("#code").val());

        if (userLoginName == "") {
            ErrorMessage($("#userLoginName"), '请输入账号');
        } else if (userEmail == "") {
            ErrorMessage($("#userEmail"), '请输入邮箱');
        } else if (userName == "") {
            ErrorMessage($("#userName"), '请输入用户名');
        } else if (userPwd == "") {
            ErrorMessage($("#userPwd"), '请输入密码');
        } else if (userPwd1 == "") {
            ErrorMessage($("#userPwd1"), '请验证密码');
        } else if (code == "") {
            ErrorMessage($("#login_verifycode"), '请输入验证码');
            $("#code").focus();
        } else if (userPwd != userPwd1) {
            ErrorMessage($("#userPwd1"), '两次输入的密码不一致');
        } else {
            var data = {
                userLoginName: userLoginName,
                userEmail: userEmail,
                userName: userName,
                userPwd: userPwd,
                verifyCode: code
            };
            CheckSignIn(data);
        }
    });
    function CheckSignIn(data) {
        $(".formmargin").find('span').hide();
        $.ajax({
            type: 'POST',
            url: '/Login/SignIn',
            data: data,
            dataType: 'JSON',
            success: function (data) {
                if (data.type == 1) {
                    window.location.href = '/Login/Index';
                } else {
                    ExistMessage(data);
                    $("#login_verifycode").trigger("click");
                }
            }
        });
    }

    function ExistMessage(data) {
        if (data.errorcode == 1) {
            ErrorMessage($('#userLoginName'), data.message);
        } else if (data.errorcode == 2) {
            ErrorMessage($('#userEmail'), data.message);
        } else if (data.errorcode == 3) {
            ErrorMessage($('#userName'), data.message);
        } else if (data.errorcode == 4) {
            ErrorMessage($('#userPwd'), data.message);
        } else if (data.errorcode == 5) {
            ErrorMessage($('#login_verifycode'), data.message);
        }
    }

    //提示信息
    function ErrorMessage(e, msg) {
        $('.txt-error').remove();
        e.focus();
        e.after('<label class="txt-error" style="color:red">' + msg+'</label>');
    }
})